import { codeValueList } from '@/api/common';
import { getGoodsList, getContactInformation } from '@/api/api';

const getDefaultState = () => {
    return {
        codeValue: '',
        scroll: new Date().getTime(), // 记录滚动刷新的时间戳
        contactInformation: {},
        goodsListParams: {},
        goodsList: { list: [], total: 0 },
        keyword: '',
        searchGoods: { list: [], total: 0 },
        dialogInquiry: {
            visible: false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
        },
        goodId: ''
    };
};

const state = getDefaultState();

const mutations = {
    // 退出登录
    signOut: (state, path) => {
        // 重置参数
        Object.assign(state, getDefaultState());
        localStorage.removeItem('mallLoginInfo');
    },
    // 修改滚动刷新的时间
    setScroll: (state, path) => {
        // 重置参数
        state.scroll = new Date().getTime();
    },
};

const actions = {
    // 获取接口code值
    getCodeValue({ commit, state }) {
        codeValueList().then(res => {
            state.codeValue = res.data;
        });
    },
    // 获取商品列表
    getGoodsList({ commit, state }, params) {
        state.goodsListParams = Object.assign({}, params);
        if (state.goodsListParams.deviceType === 5) {
            // 配件类下的一级分类
            state.goodsListParams.type = 2;
            delete state.goodsListParams.deviceType;
        }
        params.page = 1;
        params.page_size = 1000;
        if (params.firstCategory) {
            delete params.deviceType;
        }
        getGoodsList(params).then(res => {
            state.goodsList.list = res.data.list;
            state.goodsList.total = res.data.total;
            for (let i in state.goodsList.list) {
                let isnotJson = true;
                let specification = state.goodsList.list[i].specification;
                try {
                    specification = JSON.parse(specification);
                    isnotJson = false;
                } catch (e) {
                    // console.info(e)
                }
                state.goodsList.list[i].specification = isnotJson ? [] : specification;
            }

            localStorage.setItem("goodsListParams", JSON.stringify(state.goodsListParams));
        });
        console.info("商品列表：", state.goodsList);
    },
    // 搜索商品
    searchGoods({ commit, state }, params) {
        state.keyword = params.keyword;
        if (params.keyword === "") {
            state.searchGoods.list = [];
            state.searchGoods.total = 0;
        } else {
            params.page = 1;
            params.page_size = 1000;
            getGoodsList(params).then(res => {
                state.searchGoods.list = res.data.list;
                state.searchGoods.total = res.data.total;

                localStorage.setItem("keyword", state.keyword);
            });
        }
        console.info("搜索商品：", state.searchGoods);
    },
    // 记录询价的商品Id
    updateGoodId({ commit, state }, params) {
        state.goodId = params.id;
        state.dialogInquiry.visible = true;
    },
    // 更新联系方式
    updateContactInformation({ commit, state }) {
        getContactInformation({}).then((res) => {
            console.info("联系方式", res);
            state.contactInformation = res.data
        });
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};